// 统一处理请求
import axios from 'axios'

const URL = 'http://127.0.0.1:8888/api/private/v1/'
// 给axios设置全局基准路径，这样它以后发送的所有请求，都会带上这个基准路径
axios.defaults.baseURL = URL;

// 定义一个请求拦截，作用是拦截所有的请求,这里的config参数是一个请求对象，里面有很多属性
axios.interceptors.request.use(config => {
  // 先判断们是否本地有token:adminToken，若有就把token置入headers里
  var token = localStorage.getItem("adminToken")
  if (token) {
    // header的值是前后台约定的值
    config.headers["Authorization"] = token;
  }
  return config;
}, function (error) {
  return Promise.reject(error)
})

// 封装接口
/* 登陆请求 */
export const checkLogin = (params) => {
  return axios.post('login', params).then(res => res.data)
}

// 用户数据获取
export const getUserList = obj => {
  return axios.get('users', {
    params: obj
  }).then(res => res.data)
}
// 更改用户状态
export const changeState = params => {
  return axios.put(`users/${params.uid}/state/${params.type}`).then(res => res.data)
}


// 添加用户
export const addUser = obj => axios.post('users', obj).then(res => res.data)
// 删除用户
export const delUser = id => axios.delete(`users/${id}`).then(res => res.data)
// 用户编辑
export const editUser = obj => axios.put(`users/${obj.id}`, {
  email: obj.email,
  mobile: obj.mobile
}).then(res => res.data)
// 分配用户角色
export const grantUser = obj => axios.put(`users/${obj.id}/role`, {
  rid: obj.rid
}).then(res => res.data)




// 获取角色列表
export const getRoleList = () => axios.get('roles').then(res => res.data)
// 获取所有权限列表
export const getRightsList = obj => axios.get(`rights/${obj.type}`).then(res => res.data)
// 添加角色
export const addRoleList = obj => axios.post("roles", obj).then(res => res.data)
// 删除角色
export const delRoleList = id => axios.delete(`roles/${id}`).then(res => res.data)
// 编辑角色
export const editRoleList = obj => axios.put(`roles/${obj.id}`, {
  roleName: obj.roleName,
  roleDesc: obj.roleDesc
}).then(res => res.data)




// 删除指定权限
export const delRoleRight = obj => axios.delete(`roles/${obj.roleId}/rights/${obj.rightId}`)
// 角色授权
export const rolesGetRights = obj => axios.post(`roles/${obj.roleId}/rights`, {
  rids: obj.rids
})
// 菜单动态显示
export const getMenus = () => axios.get('menus').then(res => res.data)





// 获取商品列表
export const getProductList = obj => axios.get('goods', {
  params: obj
}).then(res => res.data)
// 删除商品
export const delProductList = id => axios.delete('goods/' + id).then(res => res.data)





// 获取商品分类列表
export const getCategoryList = obj => axios.get('categories', {
  params: {
    type: obj.type,
    pagesize: obj.pagesize,
    pagenum: obj.pagenum
  }
}).then(res => res.data)
// 添加商品分类
export const addCategory = obj => axios.post('categories', obj).then(res => res.data)
// 删除商品指定分类
export const delCategory = id => axios.delete(`categories/${id}`).then(res => res.data)
// 编辑商品分类
// 文件上传
// export const upData=



// 获取订单列表
export const getOrderList = obj => axios.get('orders', {
  params: obj
}).then(res => res.data)
// 编辑订单
export const editOrderList = obj => axios.put(`orders/${obj.id}`, obj).then(res => res.data)
